package com.handinfo.android.ui.window;

import java.io.DataInputStream;

import android.graphics.Bitmap;

import com.handinfo.android.DWGameManager;
import com.handinfo.android.game.GameMessage;
import com.handinfo.android.ui.IUIWindows;
import com.handinfo.android.ui.UIWindows;
import com.handinfo.android.uicontrols.DWControlsManager;
import com.handinfo.android.uicontrols.DWListener;
import com.handinfo.android.uicontrols.controls.DWBackground;
import com.handinfo.android.uicontrols.controls.DWFrame;
import com.handinfo.android.uicontrols.controls.DWImageBox;
import com.handinfo.android.uicontrols.controls.DWListSubItem;
import com.handinfo.android.uicontrols.controls.DWListbox;
import com.handinfo.android.uicontrols.controls.DWTextbox;
import com.handinfo.android.uicontrols.controls.DWTitle;
import com.handinfo.android.utils.Tools;

/**
 * 
 * @author Xiaoyingying
 * 
 * 公告窗口
 * 
 */
public class UIAnnouncement implements IUIWindows
{
	private static String TITLE = "游戏公告";

	private String m_announ_message = null; //公告
	private String[] m_announ_messaglis = null;//公告列表
	private String m_announ_specificinfos = null;//公告内容

	private DWFrame m_announ_Fram; //公告栏窗口
	private DWTitle m_announ_title;//窗口标题
	private DWTextbox m_announ_msgbox;//公告栏主体框
	private DWListbox m_announ_listbox; //公告列表框
	private DWImageBox m_announ_closewindow; //关闭窗口
	private DWBackground m_announ_msgBackgroud1;//消息背景
	private DWBackground m_announ_msgBackgroud2;
	private Bitmap closewindow;
	private Bitmap title;

	private void initFram()
	{
		m_announ_Fram = new DWFrame(DWFrame.FRAME_TYPE_FULL);

		m_announ_title = new DWTitle(TITLE, m_announ_Fram);
		m_announ_Fram.addControl(m_announ_title);
		m_announ_title.setBackground(title, false);

		m_announ_Fram.setClickClose(false);
		m_announ_closewindow = new DWImageBox(closewindow);
		m_announ_closewindow.addListener(new DWListener() {
			public void OnDoubleClick()
			{
			}

			public void OnClick()
			{
				DWControlsManager.getInstance().removeControl(m_announ_Fram);
			}
		});
		m_announ_closewindow.setNearAnchor(m_announ_title, Tools.RIGHT_VCENTER, Tools.RIGHT_VCENTER);
		m_announ_closewindow.setTouchZoomIn(20, 20);
		m_announ_Fram.addControl(m_announ_closewindow);

		m_announ_msgBackgroud1 = new DWBackground((m_announ_Fram.getShowWidth() - 60) * 2 / 5,
				m_announ_Fram.getShowHeight() - 50 -20);
		m_announ_msgBackgroud1.setNearAnchor(m_announ_Fram, Tools.LEFT_BOTTOM, Tools.LEFT_BOTTOM, 20, -20);
		m_announ_Fram.addControl(m_announ_msgBackgroud1);

		m_announ_listbox = new DWListbox(m_announ_msgBackgroud1.getShowWidth() - 60, m_announ_msgBackgroud1
				.getShowHeight() - 40);
		m_announ_listbox.setTouchIndex(0);
		m_announ_listbox.setLineSpacing(10);
		m_announ_listbox.setNearAnchor(m_announ_msgBackgroud1, Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER, -10, 0);
		m_announ_Fram.addControl(m_announ_listbox);

		m_announ_listbox.addListener(new DWListener() {
			public void OnDoubleClick()
			{
			}

			public void OnClick()
			{
				DWGameManager.getInstance().getSendMessage()
						.sendGameNoticeInfo((byte) m_announ_listbox.getTouchIndex()); //发送信息，获取该条公告的具体内容
			}
		});
		m_announ_Fram.addControl(m_announ_listbox);
		addItemMessage();

		m_announ_msgBackgroud2 = new DWBackground((m_announ_Fram.getShowWidth() - 60) * 3 / 5,m_announ_msgBackgroud1.getShowHeight() );
		m_announ_msgBackgroud2.setNearAnchor(m_announ_Fram, Tools.RIGHT_BOTTOM, Tools.RIGHT_BOTTOM, -20, -20);
		m_announ_Fram.addControl(m_announ_msgBackgroud2);
	}

	/**
	 * 显示公告
	 */
	private void setMessage()
	{
		if (m_announ_specificinfos != null)
		{
			m_announ_Fram.removeControl(m_announ_msgbox);
			m_announ_msgbox = null;
		}
		if (m_announ_specificinfos != null)
		{
			m_announ_msgbox = new DWTextbox(m_announ_specificinfos, m_announ_msgBackgroud2.getShowWidth() * 3 / 4,
					m_announ_msgBackgroud2.getShowHeight() * 3 / 4);
			m_announ_msgbox.setNearAnchor(m_announ_msgBackgroud2, Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER);
			m_announ_Fram.addControl(m_announ_msgbox);
		}
	}

	/**
	 * 添加公告列表
	 */
	private void addItemMessage()
	{
		if (m_announ_message == null)
			return;

		m_announ_listbox.removeAllSubItem();

		if (m_announ_messaglis != null)
		{
			for (int i = 0; i < m_announ_messaglis.length; i++)
			{
				DWListSubItem item = new DWListSubItem();
				DWTextbox msg = new DWTextbox(m_announ_messaglis[i], m_announ_listbox.getShowWidth());
				msg.setNearAnchor(item, Tools.LEFT_TOP, Tools.LEFT_TOP);
				item.addControls(msg);
				m_announ_listbox.addSubItem(item);
			}
		}
	}

	/**
	 * 接收公告提示
	 * */
	public void recvAnnouncement(DataInputStream dis)
	{
		DWGameManager.getInstance().getSendMessage().unlockNetwork(GameMessage.锁_公告);
		try {
			Tools.debugPrintln("recvAnnouncement");
			m_announ_message = dis.readUTF();
			byte size = dis.readByte();
			m_announ_messaglis = null;
			if (size != 0) {
				m_announ_messaglis = new String[size];
				for (int i = 0; i < size; i++) {
					m_announ_messaglis[i] = (i + 1) + "." + dis.readUTF();
				}
			}
			open((byte) 0);
		}
		catch (Exception e) {
			Tools.debug(e);
		}
	}

	/** 接收公告内容 */
	public void recvAnnouncementInfos(DataInputStream dis)
	{
		DWGameManager.getInstance().getSendMessage().unlockNetwork(GameMessage.锁_公告);
		try {
			String temp = dis.readUTF();
			m_announ_specificinfos = temp;
			Tools.debugPrintln("公告内容 : " + temp);
			setMessage();
		}
		catch (Exception e) {
			DWGameManager.getInstance().getSendMessage().unlockNetwork(GameMessage.锁_默认);
			Tools.debug(e);
		}
	}

	public void init()
	{
		closewindow = UIWindows.createImage("/img/newui/fanhuianniu_1.gnp");
		title = UIWindows.createImage("/img/newui/gonggao_1.gnp");

	}

	public void open(byte index)
	{
		initFram();
		DWGameManager.getInstance().getSendMessage().sendGameNoticeInfo((byte) 0);
		DWControlsManager.getInstance().addControl(m_announ_Fram);
	}

	public void close(byte index)
	{
		DWControlsManager.getInstance().removeControl(m_announ_Fram);
		m_announ_Fram = null;
	}

	public void dispose()
	{
	}

	public void logic()
	{
	}
}
